package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.DetailBean.NomeList;
import com.da.lon.wang.xlg.view.ScrollGridView;

import java.util.ArrayList;

public class DetailPopLvAdapter extends BaseAdapter {

    LayoutInflater inflater;
    Context context;
    ArrayList<NomeList> nomelist;
    CallBack4 myCallBack;

    public DetailPopLvAdapter(Context context, ArrayList<NomeList> nome_list,
                              CallBack4 myCallBack) {
        this.context = context;
        this.nomelist = MyApplication.getInstance().getNomelist();
        this.myCallBack = myCallBack;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return nomelist != null ? nomelist.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return nomelist.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder = new ViewHolder();

        convertView = inflater.inflate(R.layout.ocoffee_detail_pop_lv_item,
                null);

        NomeList nomeList = nomelist.get(position);

        holder.detail_pop_lv_tv = (TextView) convertView
                .findViewById(R.id.detail_pop_lv_tv);
        holder.detail_pop_lv_tv.setText(nomeList.getName());

        holder.detail_pop_lv_gv = (ScrollGridView) convertView
                .findViewById(R.id.detail_pop_lv_gv);
        holder.adapter = new DetailPopGvAdapter(context, nomeList.getValue());
        holder.detail_pop_lv_gv.setAdapter(holder.adapter);
        holder.detail_pop_lv_gv
                .setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {

                        holder.adapter.chooceState(position);
                        holder.adapter.notifyDataSetChanged();
                        myCallBack.click();
                    }
                });

        if (convertView.getTag() == null) {
            convertView.setTag(holder);
        }
        return convertView;
    }

    public interface CallBack4 {
        public void click();
    }

    class ViewHolder {

        TextView detail_pop_lv_tv;
        ScrollGridView detail_pop_lv_gv;
        DetailPopGvAdapter adapter;
    }

}
